package com.streamax.s17.mqs.business.controller;

import com.streamax.s17.mqs.api.model.dto.MsgRetrieveDTO;
import com.streamax.s17.mqs.api.model.request.MsgRetrieveRQ;
import com.streamax.s17.mqs.api.service.IMqsService;
import com.streamax.s17.mqs.business.service.IMsgQueryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Description
 * @ClassName MqsController
 * @Author liwei
 * @Date 2019/8/13 17:18
 * @Version 2.0.2
 */

@RestController
@Api(value = "消息服务http接口")
@Slf4j
public class MqsController implements IMqsService {

    public static final String MESSAGE_RETRIEVE = "信令检索";

    @Autowired
    IMsgQueryService msgRetrieveServ;

    @Override
    @RequestMapping(value = "api/v1/msg/query", method = RequestMethod.GET)
    @ApiOperation(value = "消息检索")
    public List<MsgRetrieveDTO> retrieve(@Valid @ModelAttribute MsgRetrieveRQ request) {
        log.info("-------检索请求：request:[{}]", request.toString());
        List<MsgRetrieveDTO> dtoList = msgRetrieveServ.search(request);
        log.info("-------检索响应：response:[{}]", null == dtoList ? null : list2String(dtoList));
        return dtoList;
    }

    private String list2String(List<MsgRetrieveDTO> list) {
        if (null == list || list.size() == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).toString());
            if (i < list.size() - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }
}
